<html>
<head><meta charset="utf-8"><title>Add u32::is_char() · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Add.20u32.3A.3Ais_char().html">Add u32::is_char()</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="248213547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Add%20u32%3A%3Ais_char%28%29/near/248213547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bot+ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Add.20u32.3A.3Ais_char().html#248213547">(Aug 03 2021 at 12:27)</a>:</h4>
<p>Introduce</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">is_char</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span>
</code></pre></div>
<p>as alternative for</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kt">char</span>::<span class="n">try_from</span><span class="p">().</span><span class="n">is_ok</span><span class="p">()</span><span class="w"></span>
</code></pre></div>
<p>Good or bad idea?</p>



<a name="248227871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Add%20u32%3A%3Ais_char%28%29/near/248227871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Add.20u32.3A.3Ais_char().html#248227871">(Aug 03 2021 at 14:24)</a>:</h4>
<p>What will you do if it returns true? Seems like in most cases you would then want to convert it, which means that you've done the checking logic twice.</p>



<a name="248229531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Add%20u32%3A%3Ais_char%28%29/near/248229531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Add.20u32.3A.3Ais_char().html#248229531">(Aug 03 2021 at 14:36)</a>:</h4>
<p>You may be immediately encoding it, which may be more efficient in u32 where you can do bit manipulation.</p>



<a name="248231262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Add%20u32%3A%3Ais_char%28%29/near/248231262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Add.20u32.3A.3Ais_char().html#248231262">(Aug 03 2021 at 14:48)</a>:</h4>
<p>the conversion itself is free, right? so we'd expect char::try_from().is_ok() to be equivalent to is_char() at runtime anyway.</p>
<p>This seems true in a tiny case - <a href="https://rust.godbolt.org/z/G3P3nM76P">https://rust.godbolt.org/z/G3P3nM76P</a></p>



<a name="248234347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Add%20u32%3A%3Ais_char%28%29/near/248234347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bot+ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Add.20u32.3A.3Ais_char().html#248234347">(Aug 03 2021 at 15:10)</a>:</h4>
<p>Conversion is just a transmute. Basically move <a href="https://github.com/rust-lang/rust/blob/2939249f294dd54a9ce78a8ee1f2922a44e7fb7c/library/core/src/char/convert.rs#L251">this check</a> to a const quality of life utility function.</p>
<p>Chars in const are currently major PITA.</p>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=29fc2d0117d00e1430dee4fda41153a1">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=29fc2d0117d00e1430dee4fda41153a1</a></p>



<a name="248237416"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Add%20u32%3A%3Ais_char%28%29/near/248237416" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Add.20u32.3A.3Ais_char().html#248237416">(Aug 03 2021 at 15:30)</a>:</h4>
<p>I don't think there's any reason <a href="https://doc.rust-lang.org/nightly/std/char/fn.from_u32.html">https://doc.rust-lang.org/nightly/std/char/fn.from_u32.html</a> can't be made const today (presumably moving try_from's contents into it)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>